package com.campus.web.controller;


import com.alibaba.fastjson.JSON;
import com.campus.app.service.MenusService;
import com.campus.commons.entity.Menu;
import com.campus.commons.entity.Role;
import com.campus.commons.utils.Result;
import org.apache.poi.ss.usermodel.Row;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 菜单表 前端控制器
 * </p>
 *
 * @author Rin
 * @since 2020-12-24
 */
@RestController
@RequestMapping("/menu")
public class MenuController {
    @Autowired
    private MenusService menusService;

    @GetMapping("/init")
    public String getMenu(HttpSession session) {
        Role roleId = (Role) session.getAttribute("role");
        Result<List<Menu>> result = menusService.getMenusByRole(1);
        if (result.getSuccess()) {
            //有访问后台菜单的权限;
            Map<String, Object> menu = new HashMap<>();
            Map<String, String> homeInfo = new HashMap<>();
            Map<String, String> loginInfo = new HashMap<>();
            homeInfo.put("title", "首页");
            homeInfo.put("href", "/index");
            menu.put("homeInfo", homeInfo);
            loginInfo.put("title", "校园");
            loginInfo.put("image", "/file/image/head1.png");
            loginInfo.put("href", "");
            menu.put("logoInfo", loginInfo);
            menu.put("menuInfo", result.getData());
            return JSON.toJSONString(menu);
        } else {
            //没有权限；
            return JSON.toJSONString(result);
        }
    }

    @GetMapping("/add")
    public ModelAndView toAdd() {
        return new ModelAndView("learn/menu/add");
    }

    @GetMapping("/edit")
    public ModelAndView toEdit() {
        return new ModelAndView("learn/menu/add");
    }

    @GetMapping("/")
    public ModelAndView toIndex() {
        return new ModelAndView("learn/menu/index");
    }

    @RequestMapping("/all")
    public Result getAllMenu() {
        return menusService.getAllMenus();
    }

    @PostMapping("/add")
    public Result addMenu(Menu menu) {
        return menusService.addMenu(menu);
    }

    @PostMapping("update")
    public Result updateMenu(Menu menu) {
        return menusService.upMenu(menu);
    }
    @PostMapping("/del")
    public Result delMenu(Integer id) {
        return menusService.delMenu(id);
    }




}

